package com.zq.widget.inverted.game.role.equipment.weapon.gun.bullet;

import android.content.Context;
import android.graphics.PointF;
import android.view.animation.AnimationUtils;

import com.zq.widget.inverted.game.role.damage.Damage;
import com.zq.widget.inverted.game.role.hp.HP;
import com.zq.widget.inverted.game.role.impl.base.DrawableRole;

/**
 * 子弹
 * Created by zhangqiang on 2018/3/28.
 */
public class Bullet extends DrawableRole implements Damage,HP{

    private Damage damageProxy;
    private HP hpProxy;
    private long lastTime;
    private long velocity;
    private int degree;
    private PointF startPoint;

    public Bullet(Context context, int drawableResId) {
        super(context, drawableResId);
    }

    @Override
    public void preDraw() {
        super.preDraw();

        long timeMillis = AnimationUtils.currentAnimationTimeMillis();
        if(lastTime <= 0){
            lastTime = timeMillis;
        }
        long pastTime = timeMillis - lastTime;
        lastTime = timeMillis;
        float distance = (float)pastTime / 1000 * velocity;
        int degree = this.degree % 360;
        float dx,dy;
        double radians = Math.toRadians(degree);
        dx = (float) (Math.cos(radians) * distance);
        dy = - (float) (Math.sin(radians) * distance);
        locationOffset(dx,dy);
    }

    @Override
    public boolean isFinish() {
        return super.isFinish() || getHp() <= 0 || isBeyondCanvas();
    }

    @Override
    public long damage() {
        return damageProxy.damage();
    }

    @Override
    public long totalHp() {
        return hpProxy.totalHp();
    }

    @Override
    public long getHp() {
        return hpProxy.getHp();
    }

    @Override
    public boolean addHp(long hp) {
        return hpProxy.addHp(hp);
    }

    @Override
    public boolean reduceHp(long hp) {
        return hpProxy.reduceHp(hp);
    }

    public Damage getDamageProxy() {
        return damageProxy;
    }

    public void setDamageProxy(Damage damageProxy) {
        this.damageProxy = damageProxy;
    }

    public HP getHpProxy() {
        return hpProxy;
    }

    public void setHpProxy(HP hpProxy) {
        this.hpProxy = hpProxy;
    }


    public long getVelocity() {
        return velocity;
    }

    public void setVelocity(long velocity) {
        this.velocity = velocity;
    }

    public int getDegree() {
        return degree;
    }

    public void setDegree(int degree) {
        this.degree = degree;
    }

    public PointF getStartPoint() {
        return startPoint;
    }

    public void setStartPoint(PointF startPoint) {
        this.startPoint = startPoint;
        updateLocation(startPoint);
    }
}
